<?php


namespace app\manage\model;


use think\Db;

class Examine
{
    /**
     * 入驻审核列表   type 1=待审核   2=已审核
     */
    public function  storeList($filter,$page,$page_size,$type){
        $where=[];
        $where['is_delete']=1;

        //查询传入时间当天的数据  0-24点
        $endTime = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
        $start=strtotime('0:0:0',$endTime);
        $end=strtotime('24:00:00',$endTime);

        if (!empty($endTime)) {
            //$where['create_time'] = ['<= time', $endTime];
            $where['create_time'] = [['>= time', $start], ['<= time', $end]];
        }
       /* $startTime = empty($filter['start_time']) ? 0 : strtotime($filter['start_time']);
        $endTime = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $where['create_time'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['create_time'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['create_time'] = ['<= time', $endTime];
            }
        }*/

        $keyword = empty($filter['keyword']) ? '' : $filter['keyword'];
        if(!empty($keyword)){
                $where['company_name|contact|phone'] = ['like', "%".$keyword."%"];
        }

        if($type==1){
            $where['status']=2;
        }else{
            $where['status']=['in',[1,3]];
        }

        $field='sa_id,company_name,contact,phone,address,create_time,status';
        $data=Db::name('store_apply')
            ->field($field)
            ->where($where)
            ->page($page,$page_size)
            ->select();
        //dump(Db::name('store_apply')->getLastSql());

       /* if(!empty($data)){
            foreach($data as $k=>&$v){
                $v['id']=$k+1;
            }
        }*/
        $total_num=Db::name('store_apply')
            ->where($where)
            ->count();
        $t_page=ceil($total_num / $page_size);

        $list['current_page'] = $page;
        $list['total_page'] =$t_page;
        $list['page_num'] = $page_size;
        $list['total_num'] = $total_num;
        $list['data'] = $data;

        return $list;
    }

    /**
     * 入驻详情
     */
    public function storeDetail($store_id){

        $field='company_name,credit_code,mch_id,number,investment_phone,introduce,card_just,card_back,
        business_license,fail_reason';
        $data=Db::name('store_apply')
            ->field($field)
            ->where('sa_id',$store_id)
            ->find();
        return $data;
    }

    /**
     * 审核操作   删除
     */
    public function review($store_id,$data){

        Db::name('store_apply')
            ->where('sa_id',$store_id)
            ->update($data);

        return true;
    }

    /**
     * 批量审核操作  删除
     */
    public function reviewAll($ids,$data){

        Db::name('store_apply')
            ->where(['sa_id'=>['in',$ids]])
            ->update($data);

        return true;
    }

    /**
     * 查看入驻审核拒绝原因
     */
    public function refuseDetail($store_id){
        $data=Db::name('store_apply')
            ->field('fail_reason')
            ->where('sa_id',$store_id)
            ->find();
        return $data;
    }


    /**
     * 实名认证列表   type  1=待审核   2=已审核
     */
    public function  realList($filter,$page,$page_size,$type){
        $where=[];

        //查询传入时间当天的数据  0-24点
        $endTime = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
        $start=strtotime('0:0:0',$endTime);
        $end=strtotime('24:00:00',$endTime);

        if (!empty($endTime)) {
            $where['mi.create_time'] = [['>= time', $start], ['<= time', $end]];
        }

        /*$startTime = empty($filter['start_time']) ? 0 : strtotime($filter['start_time']);
        $endTime = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $where['mi.create_time'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['mi.create_time'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['mi.create_time'] = ['<= time', $endTime];
            }
        }*/

        $keyword = empty($filter['keyword']) ? '' : $filter['keyword'];
        if(!empty($keyword)){
            $where['mi.real_name|m.mobile'] = ['like', "%".$keyword."%"];
        }

        if($type==1){
            $where['mi.status']=2;
        }else{
            $where['mi.status']=['in',[1,3]];
        }

        $field='mi.in_id,mi.real_name,m.mobile,mi.create_time,mi.status';
        $data=Db::name('member_info')->alias('mi')
            ->field($field)
            ->join('member m','m.member_id=mi.member_id')
            ->where($where)
            ->page($page,$page_size)
            ->select();

        $total_num=Db::name('member_info')->alias('mi')
            ->join('member m','m.member_id=mi.member_id')
            ->where($where)
            ->count();
        $t_page=ceil($total_num / $page_size);

        $list['current_page'] = $page;
        $list['total_page'] =$t_page;
        $list['page_num'] = $page_size;
        $list['total_num'] = $total_num;
        $list['data'] = $data;

        return $list;
    }

    /**
     * 实名认证 -----查看详情
     */
    public function realDetail($id,$field){
        $data=Db::name('member_info')->alias('mi')
            ->field($field)
            ->join('member m','m.member_id=mi.member_id')
            ->where('mi.in_id',$id)
            ->find();
        return $data;
    }

    /**
     * 实名认证 通过 拒绝 删除
     */
    public function realOperate($in_id,$data){

        Db::name('member_info')
            ->where('in_id',$in_id)
            ->update($data);

        return true;
    }

    /**
     * 实名认证  批量操作  通过 拒绝 删除
     */
    public function realOperateAll($ids,$data){
        Db::name('member_info')
            ->where(['in_id'=>['in',$ids]])
            ->update($data);
        return true;
    }

    /**
     * 股东认证列表   type 1=待审核  2=已审核
     */
    public function  shareList($filter,$page,$page_size,$type){
        $where=[];
        $where['pc.is_delete']=1;

        //查询传入时间当天的数据  0-24点
        $endTime = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
        $start=strtotime('0:0:0',$endTime);
        $end=strtotime('24:00:00',$endTime);

        if (!empty($endTime)) {
            $where['pc.create_time'] = [['>= time', $start], ['<= time', $end]];
        }

       /* $startTime = empty($filter['start_time']) ? 0 : strtotime($filter['start_time']);
        $endTime = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $where['pc.create_time'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['pc.create_time'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['pc.create_time'] = ['<= time', $endTime];
            }
        }*/

        $keyword = empty($filter['keyword']) ? '' : $filter['keyword'];
        if(!empty($keyword)){
            $where['pc.partner_name|pc.company_name'] = ['like', "%".$keyword."%"];
        }

        if($type==1){
            $where['pc.status']=2;
        }else{
            $where['pc.status']=['in',[1,3]];
        }

        $field='pc.pc_id,pc.partner_name,pc.company_name,pc.create_time,pc.status,pc.credit_code,mi.real_name';
        $data=Db::name('partner_certification')->alias('pc')
            ->field($field)
            ->join('member m','m.member_id=pc.member_id')
            ->join('member_info mi','mi.member_id=pc.member_id','left')
            ->where($where)
            ->page($page,$page_size)
            ->select();

        $total_num=Db::name('partner_certification')->alias('pc')
            ->join('member m','m.member_id=pc.member_id')
            ->join('member_info mi','mi.member_id=pc.member_id','left')
            ->where($where)
            ->count();
        $t_page=ceil($total_num / $page_size);

        $list['current_page'] = $page;
        $list['total_page'] =$t_page;
        $list['page_num'] = $page_size;
        $list['total_num'] = $total_num;
        $list['data'] = $data;

        return $list;
    }

    /**
     * 股东认证 -----查看详情
     */
    public function shareDetail($id,$field){
        $data=Db::name('partner_certification')->alias('pc')
            ->field($field)
            ->join('member m','m.member_id=pc.member_id')
            ->join('member_info mi','mi.member_id=pc.member_id','left')
            ->where('pc.pc_id',$id)
            ->find();
        return $data;
    }

    /**
     * 股东认证 通过 拒绝 删除
     */
    public function shareOperate($in_id,$data){

        Db::name('partner_certification')
            ->where('pc_id',$in_id)
            ->update($data);

        return true;
    }

    /**
     * 股东认证  批量操作  通过 拒绝 删除
     */
    public function shareOperateAll($ids,$data){
        Db::name('partner_certification')
            ->where(['pc_id'=>['in',$ids]])
            ->update($data);
        return true;
    }


    /**
     * 推客认证列表   type 1=待审核  2=已审核
     */
    public function  twitterList($filter,$page,$page_size,$type){
        $where=[];
        $where['ai.is_delete']=1;

        //查询传入时间当天的数据  0-24点
        $endTime = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
        $start=strtotime('0:0:0',$endTime);
        $end=strtotime('24:00:00',$endTime);

        if (!empty($endTime)) {
            $where['ai.create_time'] = [['>= time', $start], ['<= time', $end]];
        }

       /* $startTime = empty($filter['start_time']) ? 0 : strtotime($filter['start_time']);
        $endTime = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $where['create_time'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['create_time'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['create_time'] = ['<= time', $endTime];
            }
        }*/

        $keyword = empty($filter['keyword']) ? '' : $filter['keyword'];
        if(!empty($keyword)){
            $where['m.mobile|ai.invest_name'] = ['like', "%".$keyword."%"];
        }

        if($type==1){
            $where['ai.status']=2;
        }else{
            $where['ai.status']=['in',[1,3]];
        }

        $field='ai.ai_id as t_id,ai.invest_name as twitter_name,m.mobile as phone,ai.invest_address as address,
        ai.create_time,ai.status,ai.invest_code as credit_code';
        $data=Db::name('attract_invest')->alias('ai')
            ->field($field)
            ->join('member m','m.member_id=ai.member_id')
            ->where($where)
            ->page($page,$page_size)
            ->select();

        $total_num=Db::name('attract_invest')->alias('ai')
            ->join('member m','m.member_id=ai.member_id')
            ->where($where)
            ->count();
        $t_page=ceil($total_num / $page_size);

        $list['current_page'] = $page;
        $list['total_page'] =$t_page;
        $list['page_num'] = $page_size;
        $list['total_num'] = $total_num;
        $list['data'] = $data;

        return $list;
    }

    /**
     * 推客认证 -----查看详情
     */
    public function twitterDetail($id,$field){
        $data=Db::name('attract_invest')->alias('ai')
            ->field($field)
            ->join('member m','m.member_id=ai.member_id')
            ->where('ai.ai_id',$id)
            ->find();
        return $data;
    }

    /**
     * 推客认证 通过 拒绝 删除
     */
    public function twitterOperate($in_id,$data){

        Db::name('attract_invest')
            ->where('ai_id',$in_id)
            ->update($data);

        return true;
    }

    /**
     * 推客认证  批量操作  通过 拒绝 删除
     */
    public function twitterOperateAll($ids,$data){
        Db::name('attract_invest')
            ->where(['ai_id'=>['in',$ids]])
            ->update($data);
        return true;
    }

    /**
     * 发票认证列表   type 1=待审核  2=已审核
     */
    public function  invoiceList($filter,$page,$page_size,$type){
        $where=[];
        $where['is_delete']=1;

        if (!empty($filter['i_type']) && $filter['i_type']>0) {
            $where['i_type'] = $filter['i_type'];
        }
        //查询传入时间当天的数据  0-24点
        $endTime = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
        $start=strtotime('0:0:0',$endTime);
        $end=strtotime('24:00:00',$endTime);

        if (!empty($endTime)) {
            $where['create_time'] = [['>= time', $start], ['<= time', $end]];
        }

        /*$startTime = empty($filter['start_time']) ? 0 : strtotime($filter['start_time']);
        $endTime = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $where['create_time'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['create_time'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['create_time'] = ['<= time', $endTime];
            }
        }*/

        $keyword = empty($filter['keyword']) ? '' : $filter['keyword'];
        if(!empty($keyword)){
            $where['invoice_rise|credit_code'] = ['like', "%".$keyword."%"];
        }

        if($type==1){
            $where['status']=2;
        }else{
            $where['status']=['in',[1,3]];
        }

        $field='i_id,invoice_rise,credit_code,i_type,con_type,create_time,status,price';
        $data=Db::name('invoice')
            ->field($field)
            ->where($where)
            ->page($page,$page_size)
            ->select();

        $total_num=Db::name('invoice')
            ->where($where)
            ->count();
        $t_page=ceil($total_num / $page_size);

        $list['current_page'] = $page;
        $list['total_page'] =$t_page;
        $list['page_num'] = $page_size;
        $list['total_num'] = $total_num;
        $list['data'] = $data;

        return $list;
    }

    /**
     * 发票认证 -----查看详情
     */
    public function invoiceDetail($id,$field){
        $data=Db::name('invoice')
            ->field($field)
            ->where('i_id',$id)
            ->find();
        return $data;
    }

    /**
     * 发票认证 通过 拒绝 删除
     */
    public function invoiceOperate($in_id,$data){

        Db::name('invoice')
            ->where('i_id',$in_id)
            ->update($data);

        return true;
    }

    /**
     * 发票认证  批量操作  通过 拒绝 删除
     */
    public function invoiceOperateAll($ids,$data){
        Db::name('invoice')
            ->where(['i_id'=>['in',$ids]])
            ->update($data);
        return true;
    }
}